package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.xgkj.diyiketang.activity.data.BaoMingZheAdapter;
import com.xgkj.diyiketang.data.OrderInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.CaiPinListTask;
import com.xgkj.diyiketang.task.YiBaoMingListTask;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;
import com.xgkj.diyiketang.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/3/22.
 */
public class BaoMingRenListActivity extends BaseActivity implements PullToRefreshListView.OnRefreshListener,PullToRefreshListView.OnLoadDataListener {
    private Context mContext;
    private PullToRefreshListView mListView;
    private BaoMingZheAdapter mAdapter;
    private int cityTaskId = -1;
    private int currentPage = 1;
    private int size = 10;
    private TextView back;

    private String activityId;
    private String faqirenId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.baomingzhelist);
        mContext=this;
        initView();
    }

    private void initView(){
        Intent intent = getIntent();
        activityId = intent.getStringExtra("ACTIVEID");
        faqirenId = intent.getStringExtra("FQRID");
        PalLog.e("接到的ID", faqirenId);

        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        mListView = (PullToRefreshListView)findViewById(R.id.baomingren_list);
        mListView.init(mContext,true);
        mAdapter = new BaoMingZheAdapter(mContext);
        mListView.setAdapter(mAdapter, true);
        mAdapter.setAnchorId(faqirenId);
        mListView.setOnRefreshListener(this);
        mListView.setOnLoadDataListener(this);
        mListView.doRefresh();
    }
    private void getCityList() {
        if(cityTaskId != -1){
            return;
        }
        if(currentPage == 0){
            mListView.setNoNextPage();
            return;
        }

        YiBaoMingListTask task = new YiBaoMingListTask(activityId);
        task.addListener(cityListenter);
        cityTaskId = getEatShowApp().getTaskManager().addTask(task);
    }


    private TaskEventListener cityListenter = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if(taskId == cityTaskId){
                cityTaskId = -1;
                if(currentPage == 1){
                    mAdapter.clearData();
                    mListView.onRefreshComplete();
                    mListView.enableLoadMore();
                }else{
                    mListView.onLoadDataComplete(true);
                }
                List<OrderInfo> fansList = new ArrayList<OrderInfo>();
                if(data != null){
                    fansList = (List<OrderInfo>) data;
                }

                if(fansList != null && fansList.size()>0){
                    mAdapter.updateData(fansList);
                }

                currentPage = result;


            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if(taskId == cityTaskId){
                cityTaskId = -1;
                if(currentPage == 1){
                    mListView.onRefreshComplete();
                }else{
                    mListView.onLoadDataComplete(false);
                }
            }
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }

    };


    public void onHiddenChanged(boolean hidden) {
       onHiddenChanged(hidden);
        if(!hidden){
            //mListView.invalidate();
            if(mAdapter != null && mAdapter.getCount() == 0){
                mListView.doRefresh();
            }

        }
    }

    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {

        getCityList();
    }

    @Override
    public void onRefresh() {
         currentPage = 1;
        getCityList();
    }
}
